using System.Collections;
using System.Collections.Generic;
using Photon;
using UnityEngine;
using UnityEngine.UI;

public class PlayerOthers : Photon.MonoBehaviour
{
	public CanvasPlayer c_CanvasPlayer;

	private PlayerSetup s_PlayerSetup;

	public GameObject[] CanvasScreeen;

	public Image[] CanvasImage;

	[HideInInspector]
	public List<GameObject> m_ListPlayerKick;

	[HideInInspector]
	public List<GameObject> AllMessageInChat;

	[HideInInspector]
	public List<GameObject> AllMessageInLocalChat;

	public GameObject SpawnPosition;

	public GameObject SpawnPositionLocal;

	public GameObject m_SpawnTextMessage;

	public InputField m_ChatInput;

	private void Start()
	{
		s_PlayerSetup = GetComponent<PlayerSetup>();
		if (base.photonView.isMine || SavesAllFunctions.Local)
		{
			c_CanvasPlayer.MainCanvas.transform.SetParent(null);
			if (PlayerPrefs.HasKey("Sensivity"))
			{
				float @float = PlayerPrefs.GetFloat("Sensivity");
				@float = (float)(int)(@float * 10f) / 10f;
				c_CanvasPlayer.SensivitySlider.value = @float;
				c_CanvasPlayer.currentSensivity.text = string.Empty + @float;
				s_PlayerSetup.currentSensivity = @float;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = s_PlayerSetup.currentSensivity;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = s_PlayerSetup.currentSensivity;
			}
			c_CanvasPlayer.DashboardColos.Awake();
		}
		else if (!base.photonView.isMine)
		{
			c_CanvasPlayer.TextPlayerName.GetComponent<Text>().text = base.photonView.owner.name;
			if (s_PlayerSetup.admin)
			{
				c_CanvasPlayer.TextPlayerName.GetComponent<Text>().color = Color.red;
			}
			Object.Destroy(c_CanvasPlayer.MainCanvas);
		}
		if (SavesAllFunctions.Local)
		{
			for (int i = 0; i < c_CanvasPlayer.ChatButtonClose.Length; i++)
			{
				c_CanvasPlayer.ChatButtonClose[i].SetActive(false);
			}
		}
	}

	private void Update()
	{
		if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode && !base.photonView.isMine)
		{
			c_CanvasPlayer.TextPlayerName.transform.forward = -(Camera.main.transform.position - base.transform.position).normalized;
			return;
		}
		if (Input.GetKeyDown(KeyCode.Tab))
		{
			c_CanvasPlayer.ToolsMenuCanvas.SetActive(!c_CanvasPlayer.ToolsMenuCanvas.activeSelf);
			c_CanvasPlayer.PalyerButtonPanels.SetActive(!c_CanvasPlayer.PalyerButtonPanels.activeSelf);
			if (c_CanvasPlayer.ToolsMenuCanvas.activeSelf)
			{
				Cursor.lockState = CursorLockMode.None;
				Cursor.visible = true;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = 0f;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = 0f;
			}
			else
			{
				Cursor.lockState = CursorLockMode.Locked;
				Cursor.visible = false;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = s_PlayerSetup.currentSensivity;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = s_PlayerSetup.currentSensivity;
			}
		}
		if (Input.GetKeyDown(KeyCode.Escape))
		{
			if ((!c_CanvasPlayer.OptionMenuCanvas.activeSelf && (c_CanvasPlayer.GraphicsMenuCanvas.activeSelf || c_CanvasPlayer.DashboardColos.gameObject.activeSelf || c_CanvasPlayer.RoomMenuCanvas.activeSelf || c_CanvasPlayer.SaveMenuCanvas.activeSelf)) || c_CanvasPlayer.OptionMenuCanvas.activeSelf)
			{
				c_CanvasPlayer.OptionMenuCanvas.SetActive(false);
				c_CanvasPlayer.GraphicsMenuCanvas.SetActive(false);
				c_CanvasPlayer.DashboardColos.gameObject.SetActive(false);
				c_CanvasPlayer.RoomMenuCanvas.SetActive(false);
				c_CanvasPlayer.SaveMenuCanvas.SetActive(false);
			}
			else if (!c_CanvasPlayer.OptionMenuCanvas.activeSelf && (!c_CanvasPlayer.GraphicsMenuCanvas.activeSelf || !c_CanvasPlayer.DashboardColos.gameObject.activeSelf || !c_CanvasPlayer.RoomMenuCanvas.activeSelf || !c_CanvasPlayer.SaveMenuCanvas.activeSelf))
			{
				c_CanvasPlayer.OptionMenuCanvas.SetActive(true);
			}
		}
	}

	public void ButtonOpenToolsMenu()
	{
		c_CanvasPlayer.ToolsMenuCanvas.SetActive(!c_CanvasPlayer.ToolsMenuCanvas.activeSelf);
		if (c_CanvasPlayer.ToolsMenuCanvas.activeSelf)
		{
			if (!AdManager1.instance.ShieldAddress)
			{
				AdManager.single.ShowNative();
			}
			c_CanvasPlayer.PalyerButtonPanels.SetActive(false);
			c_CanvasPlayer.PlayerWeaponPanelsCanvas.SetActive(false);
			c_CanvasPlayer.ChatCanvas.SetActive(false);
		}
		else
		{
			c_CanvasPlayer.PalyerButtonPanels.SetActive(true);
			c_CanvasPlayer.PlayerWeaponPanelsCanvas.SetActive(true);
		}
	}

	public void OpenOrCloseWeaponPanel()
	{
		c_CanvasPlayer.WeaponPanelCanvas.SetActive(!c_CanvasPlayer.WeaponPanelCanvas.activeSelf);
	}

	public void setQuality(int level)
	{
		QualitySettings.SetQualityLevel(level);
		PlayerPrefs.SetInt("QualitySetting", level);
	}

	public void CanvasClose()
	{
		for (int i = 0; i < CanvasScreeen.Length; i++)
		{
			CanvasScreeen[i].SetActive(!CanvasScreeen[i].activeSelf);
		}
	}

	public void CanvasImage01()
	{
		CanvasScreeen[1].SetActive(false);
		CanvasScreeen[2].SetActive(false);
	}

	public void CanvasImage1()
	{
		for (int i = 0; i < CanvasImage.Length; i++)
		{
			CanvasImage[i].color = new Color(1f, 1f, 1f, 1f);
		}
		CanvasScreeen[1].SetActive(true);
		CanvasScreeen[2].SetActive(true);
	}

	public void ChangeSensivity()
	{
		float value = c_CanvasPlayer.SensivitySlider.value;
		PlayerPrefs.SetFloat("Sensivity", value);
		value = (float)(int)(value * 10f) / 10f;
		c_CanvasPlayer.currentSensivity.text = string.Empty + value;
		s_PlayerSetup.currentSensivity = value;
		s_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = s_PlayerSetup.currentSensivity;
		s_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = s_PlayerSetup.currentSensivity;
	}

	private void OnPhotonPlayerConnected(PhotonPlayer player)
	{
		if (PhotonNetwork.room == null || PhotonNetwork.offlineMode || (base.photonView.isMine && !(SpawnPosition == null)))
		{
			CheckMessage();
			GameObject gameObject = Object.Instantiate(m_SpawnTextMessage, SpawnPosition.transform.position, Quaternion.identity) as GameObject;
			GameObject gameObject2 = Object.Instantiate(m_SpawnTextMessage, SpawnPositionLocal.transform.position, Quaternion.identity) as GameObject;
			gameObject.transform.SetParent(SpawnPosition.transform);
			gameObject2.transform.SetParent(SpawnPositionLocal.transform);
			gameObject.transform.localPosition = new Vector3(1f, 1f, 1f);
			gameObject2.transform.localPosition = new Vector3(1f, 1f, 1f);
			gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
			gameObject2.transform.localScale = new Vector3(1f, 1f, 1f);
			gameObject.GetComponent<Text>().text = player.name + " Connected";
			gameObject2.GetComponent<Text>().text = player.name + " Connected";
			AllMessageInChat.Add(gameObject);
			AllMessageInLocalChat.Add(gameObject2);
			StartCoroutine(DeleteMessage(gameObject2));
		}
	}

	private void OnPhotonPlayerDisconnected(PhotonPlayer player)
	{
		if (PhotonNetwork.room == null || PhotonNetwork.offlineMode || (base.photonView.isMine && !(SpawnPosition == null)))
		{
			CheckMessage();
			Vector3 vector = SpawnChatOpenOrNo();
			GameObject gameObject = Object.Instantiate(m_SpawnTextMessage, SpawnPosition.transform.position, Quaternion.identity) as GameObject;
			GameObject gameObject2 = Object.Instantiate(m_SpawnTextMessage, SpawnPositionLocal.transform.position, Quaternion.identity) as GameObject;
			gameObject.transform.SetParent(SpawnPosition.transform);
			gameObject2.transform.SetParent(SpawnPositionLocal.transform);
			gameObject.transform.localPosition = new Vector3(1f, 1f, 1f);
			gameObject2.transform.localPosition = new Vector3(1f, 1f, 1f);
			gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
			gameObject2.transform.localScale = new Vector3(1f, 1f, 1f);
			gameObject.GetComponent<Text>().text = player.name + " Disconnect";
			gameObject2.GetComponent<Text>().text = player.name + " Disconnect";
			AllMessageInChat.Add(gameObject);
			AllMessageInLocalChat.Add(gameObject2);
			StartCoroutine(DeleteMessage(gameObject2));
		}
	}

	public Vector3 SpawnChatOpenOrNo()
	{
		if (c_CanvasPlayer.ChatCanvas.activeSelf)
		{
			return SpawnPosition.transform.position;
		}
		return SpawnPositionLocal.transform.position;
	}

	public void EnterMessage()
	{
		CheckMessage();
		string text = m_ChatInput.text;
		m_ChatInput.text = string.Empty;
		base.photonView.RPC("MessagePlayer", PhotonTargets.All, PhotonNetwork.playerName, text);
	}

	[PunRPC]
	private void MessagePlayer(string playerName, string textM)
	{
		Manager.Instance.ChatMessage(playerName, textM, s_PlayerSetup.admin);
	}

	public void CheckMessage()
	{
		for (int i = 0; i < AllMessageInLocalChat.Count; i++)
		{
			if (AllMessageInLocalChat[i].gameObject == null)
			{
				AllMessageInLocalChat.RemoveAt(i);
			}
		}
	}

	private IEnumerator DeleteMessage(GameObject message)
	{
		yield return new WaitForSeconds(10f);
		Object.Destroy(message);
	}

	public void ChatOpen()
	{
		c_CanvasPlayer.ChatSliderVal.value = 0f;
		c_CanvasPlayer.ChatCanvas.SetActive(!c_CanvasPlayer.ChatCanvas.activeSelf);
		if (c_CanvasPlayer.ChatCanvas.activeSelf)
		{
			c_CanvasPlayer.ChatLocal.SetActive(false);
		}
		else
		{
			c_CanvasPlayer.ChatLocal.SetActive(true);
		}
	}

	public void PlayerDisconnect()
	{
		s_PlayerSetup.s_PlayerGravyGun.DropObject(false);
		if (Manager.Instance.single)
		{
			Application.LoadLevel(1);
		}
		else
		{
			PhotonNetwork.Disconnect();
		}
	}

	public void Refresh()
	{
		foreach (GameObject item in m_ListPlayerKick)
		{
			Object.Destroy(item);
		}
		m_ListPlayerKick.Clear();
		for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
		{
			GameObject gameObject = Object.Instantiate(c_CanvasPlayer.KickPlayerObj, base.transform.position, Quaternion.identity) as GameObject;
			gameObject.transform.SetParent(c_CanvasPlayer.SpawnTranformKickPlayer.transform);
			gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
			PlayerKick component = gameObject.GetComponent<PlayerKick>();
			component.pLayerName.text = PhotonNetwork.playerList[i].name;
			m_ListPlayerKick.Add(gameObject);
		}
	}

	[PunRPC]
	public void KickPlayer(string name)
	{
		if (name == PhotonNetwork.playerName)
		{
			CanvasImage01();
			c_CanvasPlayer.YouKicked.SetActive(true);
			StopCoroutine("PlayerKicked");
			StartCoroutine("PlayerKicked");
		}
	}

	private IEnumerator PlayerKicked()
	{
		yield return new WaitForSeconds(3f);
		PhotonNetwork.Disconnect();
	}

	public void SaveMap()
	{
		if (c_CanvasPlayer.SaveNameText.text != string.Empty)
		{
			Manager.Instance.s_SaveAndLoad.Save(c_CanvasPlayer.SaveNameText.text);
			c_CanvasPlayer.SaveNameText.text = string.Empty;
		}
	}
}
